package com.example.tool.activtiy;

import android.animation.LayoutTransition;
import android.annotation.SuppressLint;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.util.DebugLog;

import butterknife.BindView;

import static com.example.tool.util.DebugLog.KUN;

/**
 * 描述:手势界面
 *
 * @author: liangkun
 * @date: 2019/11/21 10:10
 */
public class GestureActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.btn)
    protected Button button;
    @BindView(R.id.btn2)
    protected Button button2;
    @BindView(R.id.btn3)
    protected Button button3;
    @BindView(R.id.fl)
    protected FrameLayout frameLayout;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    private GestureDetector gd;
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int left;
    private int top;
    private int right;
    private int bottom;
    private int t;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gesture;
    }

    @Override
    protected void initView() {
//创建手势类添加监听器
//        gd = new GestureDetector(this, listener);
        button3.post(new Runnable() {
            @Override
            public void run() {
                left = button3.getLeft();
                top = button3.getTop();
                right = button3.getRight();
                bottom = button3.getBottom();

            }
        });
        tvTitle.setText("手势研究");
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initEvent() {
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        int mx = (int) (event.getRawX());
                        int my = (int) (event.getRawY() - 50);
                        v.layout(mx - button.getWidth() / 2, my - button.getHeight() / 2, mx + button.getWidth() / 2, my + button.getHeight() / 2);
                        break;
                }
                return true;
            }
        });
        button2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                MetTouchLeftClick(event);
                return true;
            }
        });
        button3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                MetTouchClick(event);
                return true;
            }
        });
    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        DebugLog.d(KUN + " onTouchEvent " + event.getAction());
        return super.onTouchEvent(event);
//        return gd.onTouchEvent(event); //手势代理
    }

    GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            DebugLog.d(KUN + " onSingleTapConfirmed " + event.getAction());
            return super.onSingleTapConfirmed(event);//单击
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            DebugLog.d(KUN + " onDoubleTap " + event.getAction());
            return super.onDoubleTap(event);//双击
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            DebugLog.d(KUN + " onFling ----");
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    };

    /**
     * @param
     * @return
     * @description 区域内
     * @author liangkun
     * @time 2019/11/21 11:51
     */
    private void MetTouchLeftClick(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //当手指按下的时候
                x1 = (int) event.getRawX();
                y1 = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int newY = (int) event.getRawY();
                int newX = (int) event.getRawX();
                int dY = newY - y1;
                int dX = newX - x1;
                int l = button2.getLeft();
                int t = button2.getTop();
                l += dX;
                t += dY;
                int r = l + button2.getWidth();
                int b = t + button2.getHeight();
                DebugLog.d(KUN + " t " + t + " top " + frameLayout.getTop() + " l " + l + " left " + frameLayout.getLeft()
                        + " r " + r + " right " + frameLayout.getRight() + " b " + b + " bottom " + frameLayout.getBottom());

                if (t > 0 && l > 0 && r < frameLayout.getRight() && b < frameLayout.getHeight()) {
                    button2.layout(l, t, r, b);
                }
                x1 = newX;
                y1 = newY;
                break;
        }
    }

    /**
     * @param
     * @return
     * @description 滑动返回
     * @author liangkun
     * @time 2019/11/21 14:08
     */
    private void MetTouchClick(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //当手指按下的时候
                x2 = (int) event.getRawX();
                y2 = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int newY = (int) event.getRawY();
                int newX = (int) event.getRawX();
                int dY = newY - y2;
                int dX = newX - x2;
                int l = button3.getLeft();
                t = button3.getTop();
                l += dX;
                t += dY;
                int r = l + button3.getWidth();
                int b = t + button3.getHeight();
                DebugLog.d(KUN + " left " + left + " top " + top + " right " + right + " bottom " + bottom);
                button3.layout(l, t, r, b);
                x2 = newX;
                y2 = newY;

                break;
            case MotionEvent.ACTION_UP:
                if (t < frameLayout.getBottom()) {
                    button3.layout(left, top, right, bottom);
                    x2 = left - button3.getWidth() / 2;
                    y2 = top - button3.getHeight() / 2;

                }
                break;
        }
    }
}
